/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DA;

import Control.DBConnector;
import Entity.Item;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author theInterloper
 */
public class itemDA {

    public static void write(Item item) throws SQLException {
        int ID =                item.getID();
        String NAME =           item.getNAME();
        int QUANTITY =          item.getQUANTITY();
        double PRICE =          item.getPRICE();
        String DESCRIPTION =    item.getDESCRIPTION();
        int ARTISTID =          item.getARTIST_ARTISTID();
        
        String insertItem = "INSERT INTO item (itemName,itemQuantity,itemPrice,itemDescription,artist_artistID) VALUES"
                + " ('"+ NAME +"','"+ QUANTITY +"','"+ PRICE +"','"+ DESCRIPTION +"','"+ ARTISTID +"');";
        
        DBConnector dbconnect = new DBConnector();
        Connection connect = dbconnect.setUpConnection();
        Statement statement = connect.createStatement();
        statement.execute(insertItem);
    }

    public static Item read(int i) throws SQLException {
        
        String getByID = "select * from item where itemID = '" + i + "'";
        
        DBConnector dbconnect = new DBConnector();
        Connection connect = dbconnect.setUpConnection();
        Statement statement = connect.createStatement();
        
        ResultSet resultset = statement.executeQuery(getByID);
        
        Item item = new Item();
        
        while(resultset.next()){
            item.setID(resultset.getInt(1));
            item.setNAME(resultset.getString(2));
            item.setQUANTITY(resultset.getInt(3));
            item.setPRICE(resultset.getDouble(4));
            item.setDESCRIPTION(resultset.getString(5));
            item.setARTIST_ARTISTID(resultset.getInt(6));   
        }
        return item;
    }
}
